package com.sfac.hqyj.springBoot.modules.test.controller;

import com.sfac.hqyj.springBoot.modules.common.entity.ResultEntity;
import com.sfac.hqyj.springBoot.modules.common.entity.SearchBean;
import com.sfac.hqyj.springBoot.modules.test.entity.Student;
import com.sfac.hqyj.springBoot.modules.test.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api")
public class StudentController {
    @Autowired
    private StudentService studentService;

    /**
     * 127.0.0.1/api/student ---- post
     * {"studentName":"hujiang"}
     * {"studentName":"hujiang", "studentCard":{"cardNo":"student_cart_00001"}}
     */
    @PostMapping(value = "/student", consumes = "application/json")
    public ResultEntity<Student> insertStudent(@RequestBody Student student) {
        return studentService.insertStudent(student);
    }

    /**
     * 127.0.0.1/api/student ---- put
     * {"id":1, "studentName":"hujiang1"}
     */
    @PutMapping(value = "/student", consumes = "application/json" )
    public ResultEntity<Student> updateStudent(@RequestBody Student student) {
        return studentService.updateStudent(student);
    }

    /**
     * 127.0.0.1/api/student/2 ---- delete
     */
    @DeleteMapping("/student/{id}")
    public ResultEntity<Object> deleteStudent(@PathVariable int id) {
        return studentService.deleteStudent(id);
    }

    /**
     * 127.0.0.1/api/student/3 ---- get
     */
    @GetMapping("/student/{id}")
    public Student getStudentById(@PathVariable int id) {
        return studentService.getStudentById(id);
    }

    /**
     * 127.0.0.1/api/students ---- post
     * {"currentPage":1, "pageSize":5, "keyWord":"hu", "orderBy":"student_name",
     * "direction":"asc"}
     */
    @PostMapping(value = "/students", consumes = "application/json")
    public Page<Student> getStudentsBySearchBean(@RequestBody SearchBean searchBean) {
        return studentService.getStudentsBySearchBean(searchBean);
    }

    /**
     * 127.0.0.1/api/student?studentName=hujiang3 ---- get
     */
    @GetMapping("/student")
    public Student findByStudentName(@RequestParam String studentName) {
        return studentService.findByStudentName(studentName);
    }
}
